#include "../GlobalGameData.h"
#include "DItem.h"

using namespace DeepClasses;

DeepClasses::DItem::DItem(DString name, wchar_t code, short color)
: DObject(name,code,color)
{
	m_isFood=false;
}

DeepClasses::DItem::~DItem()
{

}

int DeepClasses::DItem::Place( TLocation newLoc )
{
	DObject::Place(newLoc);

	DCell* cell = gameData.m_world->m_map.GetCellAt(newLoc);

	if(cell->m_mainObj!=NULL)
		cell->m_secObjs.push_back(cell->m_mainObj);
	
	cell->m_mainObj=this;
	m_pos = newLoc;

	return DE_OK;
}

int DeepClasses::DItem::Remove()
{
	DCell* cell = gameData.m_world->m_map.GetCellAt(m_pos);
	if(cell->m_mainObj==this)
	{
		if(cell->m_secObjs.empty())
			cell->m_mainObj=NULL;
		else
		{
			cell->m_mainObj = cell->m_secObjs.back();
			cell->m_secObjs.pop_back();
		}
	}
	else
	{
		std::list<DObject*>::iterator it=cell->m_secObjs.begin();
		while(it!=cell->m_secObjs.end())
		{
			if((DObject*)*it==this)
			{
				it = cell->m_secObjs.erase(it);
			}
			else
			{
				it++;
			}
		}
	}

	return DE_OK;
}